<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>档案预览</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/layer-css.css"/>
    <style>
        .organizationTree {
            width: 100% !important;
            height: -webkit-calc(100vh - 160px);
            height: -moz-calc(100vh - 160px);
            height: calc(100vh - 61px);
        }
        .centered {
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .separateInTheCenter {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }

        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }

        body {
            font-family: Source Han Sans SC, Source Han Sans SC;
        }

        .searchCriteria .layui-form-label {
            width: 64px;
            font-size: 16px;
            color: #5A6878;
        }

        .searchCriteria button {
            line-height: 31px;
            font-size: 16px;
        }

        .searchCriteria .layui-icon {
            font-size: 15px;
        }

        .toolbarButton button {
            font-size: 16px;
            border: 0px;
            border-radius: 5px 5px 5px 5px;
        }

        .toolbarButton .layui-btn {
            margin-bottom: 0px;
        }

        .layui-table-box button {
            font-size: 14px !important;
            height: 25px;
            line-height: 23px;
        }

        #moreTools a {
            color: #1890FF;
            margin-left: 20px;
        }

        .layui-table-checked {
            background-color: #9e9e9e14;
        }

        .layui-table-checked.layui-table-hover {
            background-color: #9e9e9e14 !important;
        }

        .layui-btn-container .layui-btn {
            margin-left: 15px !important;
        }

        .toolbarButton button {
            font-size: 16px;
            border: 0px;
            border-radius: 5px 5px 5px 5px;
        }

    </style>
</head>
<body class="pear-container">
<!--<div class="layui-row layui-col-space15">-->
<!--    <div class="layui-col-xs12 layui-col-md12">-->
<!--        <div class="layui-card searchCriteria">-->
<!--            <div class="layui-card-body">-->
<!--                <form class="layui-form" action="">-->
<!--                    <div class="layui-form-item">-->
<!--                        <div class="layui-form-item layui-inline">-->
<!--                            <label class="layui-form-label">分类名称</label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="text" name="typeName" placeholder="请输入分类名称" class="layui-input">-->
<!--                            </div>-->
<!--                        </div>-->
<!--                        <div class="layui-form-item layui-inline">-->
<!--                            <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="type-query">-->
<!--                                <i class="layui-icon layui-icon-search"></i>-->
<!--                                查询-->
<!--                            </button>-->
<!--                            <button type="reset" class="pear-btn pear-btn-md">-->
<!--                                <i class="layui-icon layui-icon-refresh"></i>-->
<!--                                重置-->
<!--                            </button>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </form>-->
<!--            </div>-->
<!--        </div>-->
<!--    </div>-->
<!--</div>-->
<div class="layui-row layui-col-space15">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md2">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div id="organizationTreeContent" style="overflow: auto">
                        <ul id="organizationTree" class="dtree organizationTree" data-id="9527"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md10">
<!--            <div class="layui-card toolbarButton">-->
<!--                <div class="layui-card-body">-->
<!--                    <div class="layui-btn-container centered" style="height: 65px">-->
<!--                        <button class="layui-btn layui-btn-primary layui-border-green"-->
<!--                                style="background-color: rgb(57, 143, 255, 0.1);color: #398FFF !important;"-->
<!--                                lay-on="add">新增-->
<!--                        </button>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="type-table" lay-filter="type-table"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="type-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="reload">
        <i class="layui-icon layui-icon-refresh"></i>
        重载
    </button>
</script>

<script type="text/html" id="type-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">修改</button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">删除</button>
</script>
<script type="text/html" id="type-status">
    <input type="checkbox" name="id" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="type-status" {{ d.status== 1 ? 'checked' : '' }} />
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dtree', 'treetable'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dtree = layui.dtree;
        let treetable = layui.treetable;

        dtree.render({
            elem: "#organizationTree",
            initLevel: "2", //默认展开层级为1
            line: true, // 有线树
            ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
            icon: ["0", "2"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
            method: 'get',
            url: "/archives/type/buildTypeTree"
        });

        // 区域树节点点击事件
        dtree.on("node(organizationTree)", function (obj) {
            let typeName = obj.param.context;
            treetable.search('#type-table', typeName);
            return false;
        })
        treetable.render({
            page: true,
            treeColIndex: 1,//树形图标显示在第几列
            treeSpid: 0,//最上级的父级id
            treeIdName: 'id', //id字段的名称
            treePidName: 'parentId',//父级节点字段
            // method:'post',
            skin: 'line',
            treeDefaultClose: true, //是否默认折叠
            // treeLinkage: false, //是否开启级联
            toolbar: '#type-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            elem: '#type-table',
            url: '/archives/type/list',
            id: '#type-table',
            height: 'full-30',
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'typeName', title: '分类名称', minWidth: 200},
                    {field: 'sort', title: '排序'},
                    {field: 'status', title: '是否可用', templet: '#type-status'},
                    {field: 'createTime', title: '创建时间', align: 'center'},
                    {field: 'updateTime', title: '更新时间', align: 'center'},
                    {fixed: "right", title: '操作', templet: '#type-bar', width: 150, align: 'center'}
                ]
            ]
        });


        table.on('tool(type-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(type-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'expandAll') {
                treetable.expandAll("#type-table");
            } else if (obj.event === 'foldAll') {
                treetable.foldAll("#type-table");
            } else if (obj.event === 'reload') {
                treetable.reload("#type-table");
            }
        });

        // 状态事件
        form.on('switch(type-status)', function (obj) {
            layer.confirm(obj.elem.checked ? '确定要启用该分类吗' : '确定要停用该分类吗', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                move: false,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                var status = obj.elem.checked ? 1 : 0;
                var data = {
                    id: obj.value,
                    status: status
                }
                let loading = layer.load();
                $.ajax({
                    url: '/archives/type/change/status',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                treetable.reload("#type-table");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800}, function () {
                                treetable.reload("#type-table");
                            });
                        }
                    }
                })
            }, function () {
                // 用户取消操作，恢复开关状态
                treetable.reload("#type-table");
            });
        });

        window.refresh = function (param) {
            treetable.reload('#type-table');
        }


        form.on('submit(type-query)', function (data) {
            var keyword = data.field.typeName;
            treetable.search('#type-table', keyword);
            return false;
        });

        form.on('switch(user-enable)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: ['新增分类', 'font-size: 18px;'],
                shade: 0.5,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: '/archives/type/add',
                move: false,
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: ['修改分类', 'font-size: 18px;'],
                shade: 0.5,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: '/archives/type/edit/' + obj.data['id'],
                move: false,
            });
        }

        window.remove = function (obj) {
            layer.confirm('<div>确定要删除&nbsp;&nbsp;<span style="color: #FF9200;">' + obj.data.typeName + '</span>&nbsp;&nbsp;分类吗?</div>', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm",
                move: false,
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: '/archives/type/has/child/' + obj.data.id,
                    dataType: 'json',
                    type: 'get',
                    contentType: 'application/json',
                    success: function (result) {
                        if (result.code === 200) {
                            let loading = layer.load();
                            $.ajax({
                                url: "/archives/type/delete/save/" + obj.data.id,
                                dataType: 'json',
                                type: 'delete',
                                success: function (result) {
                                    layer.close(loading);
                                    if (result.code === 200) {
                                        layer.msg(result.message, {
                                            icon: 1,
                                            time: 3000
                                        }, function () {
                                            obj.del();
                                            treetable.reload('#type-table');
                                        });
                                    } else {
                                        layer.msg(result.message, {
                                            icon: 2,
                                            time: 3000
                                        });
                                    }
                                }
                            })
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let typeName = common.checkField(obj, 'typeName');
            layer.confirm('<div>确定要删除&nbsp;&nbsp;<span style="color: #FF9200;">' + typeName + '</span>&nbsp;&nbsp;分类吗?</div>', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                move: false,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: '/archives/type/has/child/' + checkIds,
                    dataType: 'json',
                    type: 'get',
                    contentType: 'application/json',
                    success: function (result) {
                        if (result.code === 200) {
                            let loading = layer.load();
                            $.ajax({
                                url: "/archives/type/delete/save/" + checkIds,
                                dataType: 'json',
                                type: 'delete',
                                success: function (result) {
                                    layer.close(loading);
                                    if (result.code === 200) {
                                        layer.msg(result.message, {
                                            icon: 1,
                                            time: 3000
                                        }, function () {
                                            treetable.reload('#type-table');
                                        });
                                    } else {
                                        layer.msg(result.message, {
                                            icon: 2,
                                            time: 3000
                                        });
                                    }
                                }
                            })
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>
